//
//  SsehMineViewController.m
//  SsehMaster
//
//  Created by 杨阳 on 2018/8/3.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "SsehMineViewController.h"
#import "SsehMineTableViewCell.h"
#import "StarEvaluateView.h"
#import "InformationModel.h"
#import <UIImageView+WebCache.h>
#import "MyInformationViewController.h"

@interface SsehMineViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) NSArray * itemVCArr;

@property (nonatomic,strong) StarEvaluateView * starView;

@property (nonatomic,strong) InformationModel * infoModel;

@property (nonatomic,strong) UILabel * nameLabel , * phoneLabel ,* serviceLabel;

@property (nonatomic,strong) UIImageView * myPhotoImageView;

@end

NSString * const SsehMineTableViewCellID = @"SsehMineTableViewCellID";

@implementation SsehMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self layoutUI];
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = NO;
    [self getInfo];
    
    [self.myPhotoImageView sd_setImageWithURL:[NSURL URLWithString:[NSString emptyStr:DEF_PERSISTENT_GET_OBJECT(Sseh_member_img)]] placeholderImage:[UIImage imageNamed:@"photop"]];
    self.nameLabel.text = [NSString emptyStr:DEF_PERSISTENT_GET_OBJECT(Sseh_member_real_name)];
    self.phoneLabel.text = [NSString emptyStr:DEF_PERSISTENT_GET_OBJECT(SsehUserAccount)];
    self.serviceLabel.text = [NSString stringWithFormat:@"服务次数：%@",DEF_PERSISTENT_GET_OBJECT(Sseh_order_count)];
}

-(void)getInfo{
    
    NSDictionary * par = @{@"member_id":[NSString emptyStr:DEF_PERSISTENT_GET_OBJECT(SsehUserID)],@"member_token":[NSString emptyStr:DEF_PERSISTENT_GET_OBJECT(SsehUserToken)]};
    [[QYNetManager sharedManager]POST:kGetMemberWorkerDetail parameters:par andApiTyp:SsehApiTypGetOrders SuccessBlock:^(id responseObject) {
        if ([responseObject[@"status"] isEqualToString:@"ok"]) {
            self.infoModel = [InformationModel yy_modelWithDictionary:responseObject[@"data"]];
            
            [self.starView configLevel:self.infoModel.assessment_star.integerValue * 2];
            
        }
    } Failure:^(NSError *error) {
        [self showErrorTipWithTitle:SsehPoolNetStr];
    }];
    
}


-(void)layoutUI{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -STATUS_BAR_HEIGHT, VIEW_WIDTH, VIEW_HEIGHT-TAB_BAR_HEIGHT+STATUS_BAR_HEIGHT) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"SsehMineTableViewCell" bundle:nil] forCellReuseIdentifier:SsehMineTableViewCellID];
    [self.view addSubview:self.tableView];
    
    UIView * headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 165+NAVIGATION_BAR_HEIGHT)];
    headerView.backgroundColor = [UIColor appColor];
    self.tableView.tableHeaderView = headerView;
    __weak typeof (self)weakSelf = self;
    UILabel * vcTitleLabel = [UILabel new];
    vcTitleLabel.textColor = [UIColor whiteColor];
    vcTitleLabel.text = @"个人中心";
    vcTitleLabel.textAlignment = NSTextAlignmentCenter;
    vcTitleLabel.font = [UIFont systemFontOfSize:16];
    [headerView addSubview:vcTitleLabel];
    [vcTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_top).offset(STATUS_BAR_HEIGHT);
        make.centerX.equalTo(headerView.mas_centerX);
        make.height.mas_equalTo(44);
    }];
    
    //头像
    self.myPhotoImageView = [UIImageView new];
    self.myPhotoImageView.image = [UIImage imageNamed:@"photop"];
    self.myPhotoImageView.layer.cornerRadius = 30;
    self.myPhotoImageView.clipsToBounds = YES;
    [headerView addSubview:self.myPhotoImageView];
    [self.myPhotoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView).offset(10+NAVIGATION_BAR_HEIGHT);
        make.centerX.equalTo(headerView);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    
    //name
    self.nameLabel = [UILabel new];
    self.nameLabel.text = @"用户姓名";
    self.nameLabel.font = [UIFont systemFontOfSize:14];
    self.nameLabel.textColor = [UIColor whiteColor];
    [headerView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(headerView);
        make.top.equalTo(weakSelf.myPhotoImageView.mas_bottom).offset(5);
    }];
    
    //phone
    self.phoneLabel = [UILabel new];
    self.phoneLabel.text = @"12345678900";
    self.phoneLabel.font = [UIFont systemFontOfSize:14];
    self.phoneLabel.textColor = [UIColor whiteColor];
    [headerView addSubview:self.phoneLabel];
    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(headerView);
        make.top.equalTo(weakSelf.nameLabel.mas_bottom).offset(5);
    }];
    //星级
    UIView * starBgView = [UIView new];
    [headerView addSubview:starBgView];
    [starBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.phoneLabel.mas_bottom).offset(5);
        make.size.mas_equalTo(CGSizeMake(90, 16));
        make.centerX.equalTo(headerView.mas_centerX);
    }];
    //starView
    
    self.starView = [[StarEvaluateView alloc]initWithFrame:CGRectMake(0, 0, 90, 16)];
    self.starView.fullStarImage = [UIImage imageNamed:@"Star_y"];
    self.starView.emptyStarImage = [UIImage imageNamed:@"Star_g"];
    [self.starView configLevel:9];
    self.starView.userInteractionEnabled = NO;
    [starBgView addSubview:self.starView];
    
    //服务次数
    self.serviceLabel = [UILabel new];
    self.serviceLabel.text = @"服务次数：1234";
    self.serviceLabel.textColor = [UIColor whiteColor];
    self.serviceLabel.font = [UIFont systemFontOfSize:14];
    [headerView addSubview:self.serviceLabel];
    [self.serviceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(starBgView.mas_bottom).offset(5);
        make.centerX.equalTo(starBgView.mas_centerX);
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 40;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        MyInformationViewController * vc = [[MyInformationViewController alloc]init];
        vc.infoModel = self.infoModel;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        Class itemClass = NSClassFromString((NSString *)self.itemVCArr[indexPath.row]);
        [self.navigationController pushViewController:[[itemClass alloc]init] animated:YES];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SsehMineTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SsehMineTableViewCellID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSString * imageNameStr;
    NSString * titleStr;
    
    switch (indexPath.row) {
        case 0:
            imageNameStr = @"myinfo";
            titleStr = @"完善资料";
            break;
//        case 1:
//            imageNameStr = @"MyCompensation";
//            titleStr = @"我的薪资";
//            break;
//        case 2:
//            imageNameStr = @"ourPhoneNum";
//            titleStr = @"联系我们";
//            break;
        case 1:
            imageNameStr = @"aboutMe";
            titleStr = @"关于我们";
            break;
        case 2:
            imageNameStr = @"feedback";
            titleStr = @"意见反馈";
            break;
        case 3:
            imageNameStr = @"mySetting";
            titleStr = @"设置";
            break;
            
        default:
            break;
    }
    
    cell.leftImageView.image = [UIImage imageNamed:imageNameStr];
    cell.titleLabel.text = titleStr;
    
    return cell;
    
}

-(NSArray *)itemVCArr{
    
    if (_itemVCArr == nil) {
        _itemVCArr = @[@"MyInformationViewController",@"AboutUSViewController",@"FeedBackViewController",@"MySettingViewController"];
    }
    return _itemVCArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
